﻿using AutoMapper;
using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MediaAdmin.Controllers
{
    public partial class AssetController
    {
        public static class Mappings
        {
            public static void GetMap()
            {
                Mapper.CreateMap<ILocator, AssetController.LocatorSummary>()
                    .ForMember(dst => dst.Type, opt => opt.MapFrom(src => Enum.GetName(typeof(LocatorType), src.Type)))
                    .ForMember(dst1 => dst1.AccessPolicyPermissions, opt => opt.MapFrom(src1 => Enum.GetName(typeof(AccessPermissions), src1.AccessPolicy.Permissions)))
                    .ForMember(dst2 => dst2.AccessPolicyDuration, opt => opt.MapFrom(src2 => src2.AccessPolicy.LastModified.Add(src2.AccessPolicy.Duration).ToString("dd MMM hh:mm")))
                    .ForMember(dst3 => dst3.KeepAlive, opt => opt.MapFrom(src3 => true));
                Mapper.CreateMap<IFileInfo, AssetController.FileSummary>();
                Mapper.CreateMap<IAsset, AssetController.AssetSummary>()
                    .ForMember(dst => dst.State, opt => opt.MapFrom(src => Enum.GetName(typeof(AssetState), src.State)))
                    .ForMember(dst3 => dst3.Manifest, opt => opt.MapFrom(src3 => string.Empty));
                Mapper.CreateMap<IAsset, AssetController.AssetSummaryShortly>()
                    .ForMember(dst => dst.State, opt => opt.MapFrom(src => Enum.GetName(typeof(AssetState), src.State)))
                    .ForMember(dst2 => dst2.Manifest, opt => opt.Ignore());
            }
        }

        public class FileSummary
        {
            public long ContentFileSize { get; set; }

            [DisplayFormat(DataFormatString = "{dd MMM hh:mm}")]
            public DateTime Created { get; set; }
            public string Id { get; set; }
            public string Name { get; set; }

            public double SizeMb
            {
                get { return Math.Round( System.Convert.ToDouble(ContentFileSize) / 1000000, 1 ); }
            }
        }

        public class LocatorSummary
        {
            public string AccessPolicyId { get; set; }
            public string AssetId { get; set; }
            public DateTime ExpirationDateTime { get; set; }
            public string Id { get; set; }
            public string Path { get; set; }
            public string Type { get; set; }
            public string AccessPolicyPermissions {set; get;}
            public string AccessPolicyDuration { set; get; }

            //internal property for editor model
            public bool KeepAlive { set; get; }
        }

        public class AssetSummaryShortly
        {
            public string Id { get; set; }
            public DateTime Created { get; set; }
            public string Name { get; set; }
            public string State { get; set; }
            public string Manifest { set; get; }
        }

        public class AssetSummary
        {
            public string Id { get; set; }
            public DateTime Created { get; set; }
            public string Name { get; set; }
            public List<FileSummary> Files { get; set; }
            public List<LocatorSummary> Locators { get; set; }
            //public List<AssetSummary> ParentAssets { get; set; }
            public string State { get; set; }

            public string Manifest { set; get; }
        }

        public class DeleteModel
        {
            public string Name { set; get; }
            public string Id { set; get; }

            [DisplayName("delete locators and policies?")]
            public bool Cascade { set; get; }
        }

        public class CreateModel
        {
            [DisplayName("new asset name")]
            public string AssetName { set; get; }

            [DisplayName("attach 1-hour sas-locator")]
            public bool CreateLocator { set; get; }
        }

        public class EditModel
        {
            public string Id { set; get; }

            public List<LocatorSummary> AvailableLocators { set; get; }

            [DisplayName("attach new read locator (1 day)")]
            public bool NewSasReadLocator { set; get; }

            [DisplayName("attach new write locator (1 hour)")]
            public bool NewSasWriteLocator { set; get; }
            
            [DisplayName("attach new streaming locator (1 day)")]
            public bool NewStreamingLocator { set; get; }
        }
    }
}